<title>添加商品</title>
<div class="layui-card">
  <form class="layui-form" lay-filter="add-goods-form">
    <blockquote class="layui-elem-quote">选择商品分类</blockquote>
    <div class="layui-form-item">
      <label class="layui-form-label">一级分类（单选）</label>
      <div class="layui-input-block" id="first-category-pid">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">二级分类（可多选）</label>
      <div class="layui-input-block"  id="first-category-ids">
      </div>
    </div>
    <blockquote class="layui-elem-quote">填写商品信息</blockquote>
    <div class="layui-form-item">
      <label class="layui-form-label">书名</label>
      <div class="layui-input-inline" style="width: 80%;">
        <input name="title" required lay-verify="required" id="title"  placeholder="" autocomplete="off" class="layui-input">
      </div>
        <div class="layui-form-mid layui-word-aux"></div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">作者</label>
      <div class="layui-input-inline" style="width: 80%;">
        <input name="author" required lay-verify="required" id="author" placeholder="" autocomplete="off" class="layui-input">
      </div>
        <div class="layui-form-mid layui-word-aux"></div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">副标题</label>
      <div class="layui-input-inline" style="width: 80%;">
        <textarea name="sub_title" placeholder="请输入内容" id="sub_title" class="layui-textarea"></textarea>
      </div>
        <div class="layui-form-mid layui-word-aux"></div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">豆瓣评分</label>
      <div class="layui-input-inline">
        <input name="score" id="score" placeholder="" autocomplete="off" class="layui-input">
      </div>
        <div class="layui-form-mid layui-word-aux">分数对应前台页面显示的五角星</div>
    </div>
    <!--<div class="layui-form-item">
      <label class="layui-form-label">评分人数</label>
      <div class="layui-input-inline">
        <input name="score_num" id="score_num" placeholder="" autocomplete="off" class="layui-input">
      </div>
    </div>-->
    <div class="layui-form-item">
      <label class="layui-form-label">封面图</label>
      <div class="layui-input-inline">
          <button type="button" class="layui-btn" id="add-goods-upload-btn">
            <i class="layui-icon">&#xe67c;</i>上传图片
          </button>
          <div class="layui-upload-list">
            <img class="layui-upload-img" src="" id="add-goods-upload-img" style="width: 200px;">
            <input type="hidden" name="image" id="add-goods-upload-image" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
          </div>
      </div>
        <div class="layui-form-mid layui-word-aux"></div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">作者介绍</label>
      <div class="layui-input-block" style="width: 80%;">
          <textarea style="display: none;" name="author_intro" placeholder="请输入内容" id="author_intro" class="layui-textarea"></textarea>
          <div id="editor" style="margin-left: 90px;"></div>
      </div>
        <div class="layui-form-mid layui-word-aux"></div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">是否是套装</label>
      <div class="layui-input-block">
        <input type="radio" name="is_sult" lay-filter="is_sult" value="0" title="非套装" checked>
        <input type="radio" name="is_sult" lay-filter="is_sult" value="1" title="是套装">
      </div>
    </div>
    <fieldset class="layui-elem-field layui-field-title">
        <legend>出版信息</legend>
        <div class="layui-field-box">
            <div class="layui-form-item">
                <label class="layui-form-label">出版社</label>
                <div class="layui-input-inline">
                <input name="publish_house" required lay-verify="required" id="publish_house" placeholder="" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux"></div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">出版时间</label>
                <div class="layui-input-inline">
                <input type="text" name="publish_time" class="layui-input" id="publish_time" id="test-laydate-normal-cn1" placeholder="yyyy-MM-dd">
                </div>
                <div class="layui-form-mid layui-word-aux"></div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">版次</label>
                <div class="layui-input-inline">
                <input name="publish_num" required lay-verify="required" id="publish_num" placeholder="" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux"></div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">正文语种</label>
                <div class="layui-input-inline">
                <input name="publish_translator" placeholder="" id="publish_translator" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux"></div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">包装</label>
                <div class="layui-input-inline">
                <input name="publish_package" placeholder="" id="publish_package" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux"></div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">用纸</label>
                <div class="layui-input-inline">
                <input name="publish_paper" placeholder="" id="publish_paper" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux"></div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">页数</label>
                <div class="layui-input-inline">
                <input name="publish_pages" placeholder="" id="publish_pages"  class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux"></div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">开本</label>
                <div class="layui-input-inline">
                <input name="publish_open" placeholder="" id="publish_open" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux"></div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">ISBN</label>
                <div class="layui-input-inline" style="width: 70%;">
                    <input style="width: 200px;float: left;margin-right: 20px;" name="isbn" id="isbn" placeholder="" autocomplete="off" class="layui-input">
                    <button style="float: left;margin-right: 20px;" class="layui-btn layuiadmin-btn-useradmin" id="detection">检测这个ISBN是否有重复</button>
                    <span style="float: left;margin-top: 10px;" id="msg_info"></span>
                </div>

                <div class="layui-form-mid layui-word-aux"></div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">简介</label>
                <div class="layui-input-block" style="width: 80%;">
                    <textarea name="desc" style="display: none;" placeholder="请输入内容" id="desc" class="layui-textarea"></textarea>
                    <div id="editor1" style="margin-left: 90px;"></div>
                </div>
                <div class="layui-form-mid layui-word-aux"></div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">目录</label>
                <div class="layui-input-block" style="width: 80%;">
                    <textarea name="catalog" style="display: none;" placeholder="请输入内容" id="catalog" class="layui-textarea"></textarea>
                    <div id="editor2" style="margin-left: 90px;"></div>
                </div>
                <div class="layui-form-mid layui-word-aux"></div>
            </div>
        </div>
    </fieldset>
    <!-- 价格库存 -->
	<blockquote class="layui-elem-quote">价格库存</blockquote>
	<button style="float: right;margin-right: 20px;" class="layui-btn layuiadmin-btn-useradmin" id="allPrice">计算价格</button>
    <div class="layui-form-item">
      <label class="layui-form-label">图书原价(划线价)</label>
      <div class="layui-input-inline">
        <input name="old_price" required lay-verify="required" id="old_price" placeholder="" autocomplete="off" class="layui-input">
      </div>
        <div class="layui-form-mid layui-word-aux"></div>
    </div>
    <fieldset class="layui-elem-field layui-field-title">
		<div class="conter">
			<dl class="bookstype">
					<dt>图书品相</dt>
					<dd>全新书</dd>
					<dd>品相良好</dd>
					<dd><span style="color: #ccc">品相中等：</span>轻度污渍或磨损</dd>
					<dd><span style="color: #ccc">品相中等：</span>轻度破损或磨损</dd>
					<dd><span style="color: #ccc">品相中等：</span>轻度变形或折痕</dd>
					<dd><span style="color: #ccc">品相中等：</span>可拆外封套缺失</dd>
					<dd><span style="color: #ccc">品相中等：</span>少量划线或标注</dd>
					<dd><span style="color: #ccc">品相中等：</span>轻度泛黄或褪色</dd>
				</dl>
				<dl class="bookstype">
					<dt>售卖价格</dt>
					<dd><span class="sp5">￥</span><input type="text" value="0" name="new_book_price" id="new_book_price"><span class="sp6"><span id="new_book_discount">0</span>折</span></dd>
					<dd><span class="sp5">￥</span><input type="text" value="0" name="good_book_price" id="good_book_price"><span class="sp6"><span id="good_book_discount">0</span>折</span></dd>
					<dd><span class="sp5">￥</span><input type="text" value="0" name="medium_book_price" id="medium_book_price"><span class="sp6"><span id="medium_book_discount">0</span>折</span></dd>
					<dd><span class="sp5">￥</span><input type="text" value="0" name="medium_book_price2" id="medium_book_price2"><span class="sp6"><span id="medium_book_discount2">0</span>折</span></dd>
					<dd><span class="sp5">￥</span><input type="text" value="0" name="medium_book_price3" id="medium_book_price3"><span class="sp6"><span id="medium_book_discount3">0</span>折</span></dd>
					<dd><span class="sp5">￥</span><input type="text" value="0" name="medium_book_price4" id="medium_book_price4"><span class="sp6"><span id="medium_book_discount4">0</span>折</span></dd>
					<dd><span class="sp5">￥</span><input type="text" value="0" name="medium_book_price5" id="medium_book_price5"><span class="sp6"><span id="medium_book_discount5">0</span>折</span></dd>
					<dd><span class="sp5">￥</span><input type="text" value="0" name="medium_book_price6" id="medium_book_price6"><span class="sp6"><span id="medium_book_discount6">0</span>折</span></dd>
				</dl>
				<dl class="bookstype">
					<dt>库存</dt>
					<dd><input type="text" value="0" name="new_book_stock" id="new_book_stock"></dd>
					<dd><input type="text" value="0" name="good_book_stock" id="good_book_stock"></dd>
					<dd><input type="text" value="0" name="medium_book_stock" id="medium_book_stock"></dd>
					<dd><input type="text" value="0" name="medium_book_stock2" id="medium_book_stock2"></dd>
					<dd><input type="text" value="0" name="medium_book_stock3" id="medium_book_stock3"></dd>
					<dd><input type="text" value="0" name="medium_book_stock4" id="medium_book_stock4"></dd>
					<dd><input type="text" value="0" name="medium_book_stock5" id="medium_book_stock5"></dd>
					<dd><input type="text" value="0" name="medium_book_stock6" id="medium_book_stock6"></dd>
				</dl>
		</div>

        <!-- <div class="layui-field-box">
            <div class="layui-form-item">
                <label class="layui-form-label">价格</label>
                <div class="layui-input-inline">
                <span class="sp3">￥</span>
                <input name="new_book_price" required lay-verify="required" id="new_book_price" placeholder="" autocomplete="off" class="layui-input int1">
                </div>
                <div class="layui-form-mid layui-word-aux"><span class="discount" id="new_book_price_discount"></span>折</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">库存</label>
                <div class="layui-input-inline">
                <input name="new_book_stock" id="new_book_stock" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux"></div>
            </div>
        </div> -->
    </fieldset>
    <!-- <fieldset class="layui-elem-field layui-field-title">
        <legend>品相良好</legend>
        <div class="layui-field-box">
            <div class="layui-form-item">
                <label class="layui-form-label">价格</label>
                <div class="layui-input-inline">
                	<span class="sp3">￥</span>
                  <input name="good_book_price" required lay-verify="required" id="good_book_price" placeholder="" autocomplete="off" class="layui-input int1">
                </div>
                <div class="layui-form-mid layui-word-aux"><span class="discount" id="good_book_price_discount"></span>折</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">库存</label>
                <div class="layui-input-inline">
                <input name="good_book_stock" id="good_book_stock" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux"></div>
            </div>
        </div>
    </fieldset>
    <fieldset class="layui-elem-field layui-field-title">
        <legend>品相中等</legend>
        <div class="layui-field-box">
            <div class="layui-form-item">
                <label class="layui-form-label">价格</label>
                <div class="layui-input-inline">
                	<span class="sp3">￥</span>
                <input name="medium_book_price" required lay-verify="required" id="medium_book_price" placeholder="" autocomplete="off" class="layui-input int1">
                </div>
                <div class="layui-form-mid layui-word-aux"><span class="discount" id="medium_book_price_discount"></span>折</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">库存</label>
                <div class="layui-input-inline">
                <input name="medium_book_stock" id="medium_book_stock" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux"></div>
            </div>
        </div>
    </fieldset> -->
    <!-- 回收设置 -->
    <blockquote class="layui-elem-quote">回收设置</blockquote>
    <div class="layui-form-item">
      <label class="layui-form-label">是否支持回收</label>
      <div class="layui-input-block">
        <input type="radio" name="is_recycle" lay-filter="is_recycle" value="1" title="是" checked>
        <input type="radio" name="is_recycle" lay-filter="is_recycle" value="2" title="否">
      </div>
    </div>
    <div class="layui-form-item" id="is_recycle">
        <label class="layui-form-label">回收价格</label>
        <div class="layui-input-inline">
        	<span class="sp3">￥</span>
        <input name="recyle_price" id="recyle_price" placeholder="" autocomplete="off" class="layui-input int1">
        </div>
        <div class="layui-form-mid layui-word-aux">原价的<span class="discount" id="recyle_price_discount"></span>折</div>
    </div>
    <!-- 设置推荐位 -->
    <blockquote class="layui-elem-quote">设置推荐位</blockquote>
    <div class="layui-form-item">
      <label class="layui-form-label">把这本书推荐到（可多选）</label>
      <div class="layui-input-block" id="first-homepage-recommend-module-ids">
      </div>
    </div>

    <div class="layui-form-item" style="text-align: center">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo" id="formDemo">立即提交</button>
            <button class="layui-btn" lay-submit lay-filter="formDemo1" id="formDemo1" style="display: none;">编辑提交</button>
            <button class="layui-btn" id="edit_comment" style="display: none;">编辑系统评论</button>
        </div>
    </div>
  </form>

<script>
    //Demo
  layui.use(['form', 'upload','laydate'], function(){
  	var laydate = layui.laydate;
    var form = layui.form;
    var $ = layui.$;
    var upload = layui.upload;
    var book_id = layui.router().search.book_id;
    laydate.render({
      elem: '#test-laydate-normal-cn1'
    });
    //上传图片
	  upload.render({
	    elem: '#add-goods-upload-btn'
	    ,url: 'https://bookshop.dingdangtoy.com/api/admin/image/store'
	    ,accept: 'images'
	    ,method: 'get'
	    ,acceptMime: 'image/*'
	    ,done: function(res){
	    	console.log(res.data.url)
	    	$('#add-goods-upload-img').attr('src',res.data.url);
	    	$('#add-goods-upload-image').val(res.data.url)
	      //$(this.item).prev("div").children("input").val(res.data.url)
	    }
	  });
	  // 获取 一级分类
    $.ajax({
		    type: "GET",
		    dataType: "json",
		    url: "https://bookshop.dingdangtoy.com/api/admin/category/index",
		    data: {},
		    headers: { //通过 request 头传递
	          'access-token': layui.data('layuiAdmin').access_token
	        },
			success: function(data){
				var _html = '';
				for(var i=0;i<data.data.length;i++){
					_html += '<input type="radio" name="category_pid" lay-filter="category-pid" value="'+data.data[i].id+'" title="'+data.data[i].title+'">'
				}
	      $("#first-category-pid").append(_html);
				form.render();
			},
			error: function(msg){
			  console.log(msg)
			}
		});
		// 计算折扣
    var v1 = '';
    var v2 = '';

    $('#old_price').keyup(function(){
        v1 = $(this).val();
    });
    $('#new_book_price').keyup(function(){
        v2 = $(this).val();
        if(v1 == ''){//验证第一个是否输入
            //alert('第一个文本框不为空');
            return false;
        }else{
            if(v2 == ''){
                //alert('第二个文本框不为空');
            }else{
            	if(parseFloat(v2)/parseFloat(v1) == 1) {
            		$('#new_book_discount').text('0');
            	} else {
            		$('#new_book_discount').text(toDecimal((parseFloat(v2)/parseFloat(v1))*10,1));
            	}

            }
        }
    });
    $('#good_book_price').keyup(function(){
        v2 = $(this).val();
        if(v1 == ''){//验证第一个是否输入
            //alert('第一个文本框不为空');
            return false;
        }else{
            if(v2 == ''){
                //alert('第二个文本框不为空');
            }else{
            	if(parseFloat(v2)/parseFloat(v1) == 1) {
            		$('#good_book_discount').text('0');
            	} else {
            		$('#good_book_discount').text(toDecimal((parseFloat(v2)/parseFloat(v1))*10,1));
            	}

            }
        }
    });
    $('#medium_book_price').keyup(function(){
        v2 = $(this).val();
        if(v1 == ''){//验证第一个是否输入
            //alert('第一个文本框不为空');
            return false;
        }else{
            if(v2 == ''){
                //alert('第二个文本框不为空');
            }else{
            	if(parseFloat(v2)/parseFloat(v1) == 1) {
            		$('#medium_book_discount').text('0');
            	} else {
            		$('#medium_book_discount').text(toDecimal((parseFloat(v2)/parseFloat(v1))*10,1));
            	}

            }
        }
    });
    $('#medium_book_price2').keyup(function(){
        v2 = $(this).val();
        if(v1 == ''){//验证第一个是否输入
            //alert('第一个文本框不为空');
            return false;
        }else{
            if(v2 == ''){
                //alert('第二个文本框不为空');
            }else{
            	if(parseFloat(v2)/parseFloat(v1) == 1) {
            		$('#medium_book_discount2').text('0');
            	} else {
            		$('#medium_book_discount2').text(toDecimal((parseFloat(v2)/parseFloat(v1))*10,1));
            	}

            }
        }
    });
    $('#medium_book_price3').keyup(function(){
        v2 = $(this).val();
        if(v1 == ''){//验证第一个是否输入
            //alert('第一个文本框不为空');
            return false;
        }else{
            if(v2 == ''){
                //alert('第二个文本框不为空');
            }else{
            	if(parseFloat(v2)/parseFloat(v1) == 1) {
            		$('#medium_book_discount3').text('0');
            	} else {
            		$('#medium_book_discount3').text(toDecimal((parseFloat(v2)/parseFloat(v1))*10,1));
            	}

            }
        }
    });
    $('#medium_book_price4').keyup(function(){
        v2 = $(this).val();
        if(v1 == ''){//验证第一个是否输入
            //alert('第一个文本框不为空');
            return false;
        }else{
            if(v2 == ''){
                //alert('第二个文本框不为空');
            }else{
            	if(parseFloat(v2)/parseFloat(v1) == 1) {
            		$('#medium_book_discount4').text('0');
            	} else {
            		$('#medium_book_discount4').text(toDecimal((parseFloat(v2)/parseFloat(v1))*10,1));
            	}

            }
        }
    });
    $('#medium_book_price5').keyup(function(){
        v2 = $(this).val();
        if(v1 == ''){//验证第一个是否输入
            //alert('第一个文本框不为空');
            return false;
        }else{
            if(v2 == ''){
                //alert('第二个文本框不为空');
            }else{
            	if(parseFloat(v2)/parseFloat(v1) == 1) {
            		$('#medium_book_discount5').text('0');
            	} else {
            		$('#medium_book_discount5').text(toDecimal((parseFloat(v2)/parseFloat(v1))*10,1));
            	}

            }
        }
    });
    $('#medium_book_price6').keyup(function(){
        v2 = $(this).val();
        if(v1 == ''){//验证第一个是否输入
            //alert('第一个文本框不为空');
            return false;
        }else{
            if(v2 == ''){
                //alert('第二个文本框不为空');
            }else{
            	if(parseFloat(v2)/parseFloat(v1) == 1) {
            		$('#medium_book_discount6').text('0');
            	} else {
            		$('#medium_book_discount6').text(toDecimal((parseFloat(v2)/parseFloat(v1))*10,1));
            	}

            }
        }
    });
    $('#recyle_price').keyup(function(){
        v2 = $(this).val();
        if(v1 == ''){//验证第一个是否输入
            //alert('第一个文本框不为空');
            return false;
        }else{
            if(v2 == ''){
                //alert('第二个文本框不为空');
            }else{
            	if(parseFloat(v2)/parseFloat(v1) == 1) {
            		$('#recyle_price_discount').text('0');
            	} else {
            		$('#recyle_price_discount').text((((parseFloat(v2)/parseFloat(v1))*10)).toFixed(1));
            	}

            }
        }
    });
    // 获取推荐位
    $.ajax({
		    type: "GET",
		    dataType: "json",
		    url: "https://bookshop.dingdangtoy.com/api/admin/homepage_recommend_module/index",
		    data: {},
		    headers: { //通过 request 头传递
	          'access-token': layui.data('layuiAdmin').access_token
	        },
			success: function(data){
				var _html = '';
				for(var i=0;i<data.data.length;i++){
					_html += '<input type="checkbox" name="homepage_recommend_module_ids" value="'+data.data[i].id+'" title="'+data.data[i].title+'">'
				}
	      $("#first-homepage-recommend-module-ids").append(_html);
				form.render();
			},
			error: function(msg){
			  console.log(msg)
			}
		});
// 获取书籍详情
	  if(book_id){
	  	$('#formDemo').hide();
	  	$('#formDemo1').show();
	  	$('#edit_comment').show();
	  	  setTimeout(function(){
	  	  	 $.ajax({
			    type: "GET",
			    dataType: "json",
			    url: "https://bookshop.dingdangtoy.com/api/admin/book/show?id="+book_id,
			    data: {},
			    headers: { //通过 request 头传递
		          'access-token': layui.data('layuiAdmin').access_token
		        },
				success: function(data){
					console.log(data.data)
					$('#title').val(data.data.title);
					$('#author').val(data.data.author);
					$('#sub_title').val(data.data.sub_title);
					$('#score').val(data.data.score);
					$('#add-goods-upload-img').attr('src',data.data.image)
					$('#add-goods-upload-image').val(data.data.image);
					$('#author_intro').val(data.data.author_intro);
					$('#publish_house').val(data.data.publish_house);
					$('#publish_time').val(data.data.publish_time);
					$('#publish_num').val(data.data.publish_num);
					$('#publish_translator').val(data.data.publish_translator);
					$('#publish_package').val(data.data.publish_package);
					$('#publish_paper').val(data.data.publish_paper);
					$('#publish_pages').val(data.data.publish_pages);
					$('#publish_open').val(data.data.publish_open);
					$('#isbn').val(data.data.isbn);
					$('#desc').val(data.data.desc);
					$('#catalog').val(data.data.catalog);
					$('#old_price').val(data.data.old_price);
					$('#new_book_price').val(data.data.new_book_price);
					$('#new_book_stock').val(data.data.new_book_stock);
					$('#good_book_price').val(data.data.good_book_price);
					$('#good_book_stock').val(data.data.good_book_stock);
					$('#medium_book_price').val(data.data.medium_book_price);
					$('#medium_book_stock').val(data.data.medium_book_stock);
					$('#medium_book_price2').val(data.data.medium_book_price2);
					$('#medium_book_stock2').val(data.data.medium_book_stock2);

					$('#medium_book_price3').val(data.data.medium_book_price3);
					$('#medium_book_stock3').val(data.data.medium_book_stock3);

					$('#medium_book_price4').val(data.data.medium_book_price4);
					$('#medium_book_stock4').val(data.data.medium_book_stock4);

					$('#medium_book_price5').val(data.data.medium_book_price5);
					$('#medium_book_stock5').val(data.data.medium_book_stock5);

					$('#medium_book_price6').val(data.data.medium_book_price6);
					$('#medium_book_stock6').val(data.data.medium_book_stock6);

					$('#recyle_price').val(data.data.recyle_price);
//					$('#score_num').val(data.data.score_num);
					// 计算折扣
					v1 = data.data.old_price;
					$('#new_book_price_discount').text((((parseFloat(data.data.new_book_price)/parseFloat(data.data.old_price))*10)).toFixed(1));
					$('#good_book_price_discount').text((((parseFloat(data.data.good_book_price)/parseFloat(data.data.old_price))*10)).toFixed(1));
					$('#medium_book_price_discount').text((((parseFloat(data.data.medium_book_price)/parseFloat(data.data.old_price))*10)).toFixed(1));
					$('#recyle_price_discount').text((((parseFloat(data.data.recyle_price)/parseFloat(data.data.old_price))*10)).toFixed(1));
					var rng = data.data.category_pid;
					$("input[name='category_pid'][value="+rng+"]").attr("checked",true);
					var ig = data.data.is_recycle;
					var igg = data.data.is_sult;
					$("input[name='is_recycle'][value="+ig+"]").attr("checked",true);
					$("input[name='is_sult'][value="+igg+"]").attr("checked",true);
					var flavors = data.data.category_ids.split(",");
					var omg = data.data.homepage_recommend_module_ids.split(",");
			    for(var i=0;i<omg.length;i++){
							$("input[name='homepage_recommend_module_ids']").each(function(){
								if($(this).val()==omg[i]){
									$(this).attr("checked","checked");
								}
							})
					}
					// 获取二级分类
			    $.ajax({
					    type: "GET",
					    dataType: "json",
					    url: "https://bookshop.dingdangtoy.com/api/admin/category/list?pid="+ rng,
					    data: {},
					    headers: { //通过 request 头传递
				          'access-token': layui.data('layuiAdmin').access_token
				        },
						success: function(data){
							var _html1 = '';
							for(var i=0;i<data.data.length;i++){
								if(parseInt(data.data[i].id) !==0) {
										_html1 += '<input type="checkbox" name="category_ids" value="'+data.data[i].id+'" title="'+data.data[i].title+'">'
									}

							}
				      $("#first-category-ids").append(_html1);
				      for(var i=0;i<flavors.length;i++){
								$("input[name='category_ids']").each(function(){
									if($(this).val()==flavors[i]){
										$(this).attr("checked","checked");
									}
								})
							}
							form.render();
						},
						error: function(msg){
						  console.log(msg)
						}
					});
					form.render();
				},
				error: function(msg){
				  console.log(msg)
				}
			});


	  	  },600)
	    }
		form.on('radio(category-pid)', function(data){
		  $("#first-category-ids").empty();
		  // 获取二级分类
	    $.ajax({
			    type: "GET",
			    dataType: "json",
			    url: "https://bookshop.dingdangtoy.com/api/admin/category/list?pid="+data.value,
			    data: {},
			    headers: { //通过 request 头传递
		          'access-token': layui.data('layuiAdmin').access_token
		        },
				success: function(data){
					var _html1 = '';
					for(var i=0;i<data.data.length;i++){
						if(parseInt(data.data[i].id) !==0){
							_html1 += '<input type="checkbox" name="category_ids" value="'+data.data[i].id+'" title="'+data.data[i].title+'">'
						}

					}
		      $("#first-category-ids").append(_html1);
					form.render();
				},
				error: function(msg){
				  console.log(msg)
				}
			});
		});
      form.render(null, 'add-goods-form')
    //监听提交
	  form.on('submit(formDemo)', function(data){
	     var arr = new Array();
	     var arr_z = new Array();
	     $("input:checkbox[name='category_ids']:checked").each(function(i){
	       arr[i] = $(this).val();
	    });
	    $("input:checkbox[name='homepage_recommend_module_ids']:checked").each(function(s){
	       arr_z[s] = $(this).val();
	    });
	    data.field.category_ids = arr.join(",");//将数组合并成字符串
	    data.field.homepage_recommend_module_ids = arr_z.join(",");//将数组合并成字符串
        //执行 Ajax 后重载
   	  $.ajax({
			    type: "POST",
			    dataType: "json",
			    url: "https://bookshop.dingdangtoy.com/api/admin/book/store",
			    data: {
			    	category_pid:data.field.category_pid,
			    	category_ids:data.field.category_ids,
			    	title:data.field.title,
			    	author:data.field.author,
			    	sub_title:data.field.sub_title,
			    	score:data.field.score,
			    	image:data.field.image,
			    	author_intro:data.field.author_intro,
			    	publish_house:data.field.publish_house,
			    	publish_time:data.field.publish_time,
			    	publish_num:data.field.publish_num,
			    	publish_translator:data.field.publish_translator,
			    	publish_package:data.field.publish_package,
			    	publish_paper:data.field.publish_paper,
			    	publish_pages:data.field.publish_pages,
			    	publish_open:data.field.publish_open,
			    	isbn:data.field.isbn,
			    	desc:data.field.desc,
			    	catalog:data.field.catalog,
			    	old_price:data.field.old_price,
			    	new_book_price:data.field.new_book_price,
			    	new_book_stock:data.field.new_book_stock,
			    	good_book_price:data.field.good_book_price,
			    	good_book_stock:data.field.good_book_stock,
			    	medium_book_price2:data.field.medium_book_price2,
					medium_book_stock2:data.field.medium_book_stock2,
					medium_book_price3:data.field.medium_book_price3,
					medium_book_stock3:data.field.medium_book_stock3,
					medium_book_price4:data.field.medium_book_price4,
					medium_book_stock4:data.field.medium_book_stock4,
					medium_book_price5:data.field.medium_book_price5,
					medium_book_stock5:data.field.medium_book_stock5,
					medium_book_price6:data.field.medium_book_price6,
					medium_book_stock6:data.field.medium_book_stock6,
					medium_book_price:data.field.medium_book_price,
			    	medium_book_stock:data.field.medium_book_stock,
			    	is_recycle:data.field.is_recycle,
			    	is_sult:data.field.is_sult,
			    	recyle_price:data.field.recyle_price,
			    	homepage_recommend_module_ids:data.field.homepage_recommend_module_ids
			    },
			    headers: { //通过 request 头传递
	              'access-token': layui.data('layuiAdmin').access_token
	            },
				success: function(data){
					var book_id1 = data.data.book_id;
				  if(data.code == 0){
				  	layer.open({
			        type: 1
			        ,title: false //不显示标题栏
			        ,closeBtn: false
			        ,area: '300px;'
			        ,shade: 0.8
			        ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
			        ,btn: ['去添加', '不添加']
			        ,btnAlign: 'c'
			        ,moveType: 1 //拖拽模式，0或者1
			        ,content: '<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">已保存成功，是否添去加系统评论？</div>'
			        ,success: function(layero){
			          var btn = layero.find('.layui-layer-btn');
			          btn.find('.layui-layer-btn0').attr('lay-href','/goods/add-goods/add-comment/book_id=' + book_id1);
			          btn.find('.layui-layer-btn1').attr('lay-href','/goods/goods-list/');
			        }
			    });

				  } else {
				  	layer.msg(data.message);
				  }

				},
				error: function(msg){
				  console.log(msg)
				}
			});

	    return false;
	  });
	  // 监听编辑提交
	  form.on('submit(formDemo1)', function(data){
	     var arr = new Array();
	     var arr_z = new Array();
	     $("input:checkbox[name='category_ids']:checked").each(function(i){
	       arr[i] = $(this).val();
	    });
	    $("input:checkbox[name='homepage_recommend_module_ids']:checked").each(function(s){
	       arr_z[s] = $(this).val();
	    });
	    data.field.category_ids = arr.join(",");//将数组合并成字符串
	    data.field.homepage_recommend_module_ids = arr_z.join(",");//将数组合并成字符串
        //执行 Ajax 后重载
   	  $.ajax({
			    type: "PUT",
			    dataType: "json",
			    url: "https://bookshop.dingdangtoy.com/api/admin/book/update",
			    data: {
			    	id:book_id,
			    	category_pid:data.field.category_pid,
			    	category_ids:data.field.category_ids,
			    	title:data.field.title,
			    	author:data.field.author,
			    	sub_title:data.field.sub_title,
			    	score:data.field.score,
			    	image:data.field.image,
			    	author_intro:data.field.author_intro,
			    	publish_house:data.field.publish_house,
			    	publish_time:data.field.publish_time,
			    	publish_num:data.field.publish_num,
			    	publish_translator:data.field.publish_translator,
			    	publish_package:data.field.publish_package,
			    	publish_paper:data.field.publish_paper,
			    	publish_pages:data.field.publish_pages,
			    	publish_open:data.field.publish_open,
			    	isbn:data.field.isbn,
			    	desc:data.field.desc,
			    	catalog:data.field.catalog,
			    	old_price:data.field.old_price,
			    	new_book_price:data.field.new_book_price,
			    	new_book_stock:data.field.new_book_stock,
			    	good_book_price:data.field.good_book_price,
			    	good_book_stock:data.field.good_book_stock,
			    	medium_book_price:data.field.medium_book_price,
			    	medium_book_stock:data.field.medium_book_stock,
			    	is_recycle:data.field.is_recycle,
			    	is_sult:data.field.is_sult,
			    	recyle_price:data.field.recyle_price,
			    	homepage_recommend_module_ids:data.field.homepage_recommend_module_ids
			    },
			    headers: { //通过 request 头传递
	              'access-token': layui.data('layuiAdmin').access_token
	            },
				success: function(data){
					var book_id1 = data.data.book_id;
				  if(data.code == 0){
				  	layer.msg('操作成功');
				  	location.hash = '/goods/goods-list/';
				  } else {
				  	layer.msg(data.message);
				  }

				},
				error: function(msg){
				  console.log(msg)
				}
			});

	    return false;
	  });

	  // 跳转新增评论
	  $('#edit_comment').on('click', function(){
	  	location.hash = '/goods/add-goods/add-comment/book_id='+book_id;
	  	return false;
	  })
	  function toDecimal(num, n) {
			n = n ? parseInt(n) : 0;
			if(n <= 0) {
				return Math.round(num);
			}
			num = Math.round(num * Math.pow(10, n)) / Math.pow(10, n); //四舍五入
			num = Number(num).toFixed(n); //补足位数
			return num;
		}
	  // 计算总价
	  // 计算总价
	  $('#allPrice').on('click', function(){
        var price1 = parseFloat($('#old_price').val());
		var price2 = parseFloat($('#new_book_price').val());
		var price3 = parseFloat($('#good_book_price').val());
		var pax = price3 + (price1*0.13);
		$('#new_book_price').val(toDecimal(pax,1));
		$('#medium_book_price').val(toDecimal(price3*0.85,1));
		$('#medium_book_price2').val(toDecimal(price3*0.75,1));
		$('#medium_book_price3').val(toDecimal(price3*0.7,1));
		$('#medium_book_price4').val(toDecimal(price3*0.8,1));
		$('#medium_book_price5').val(toDecimal(price3*0.75,1));
		$('#medium_book_price6').val(toDecimal(price3*0.7,1));
		$('#new_book_discount').text(toDecimal((parseFloat($('#new_book_price').val())/price1)*10,1));
		$('#good_book_discount').text(toDecimal((price3/price1)*10,1));
		$('#medium_book_discount').text(toDecimal((parseFloat($('#medium_book_price').val())/price1)*10,1));
		$('#medium_book_discount2').text(toDecimal((parseFloat($('#medium_book_price2').val())/price1)*10,1));
		$('#medium_book_discount3').text(toDecimal((parseFloat($('#medium_book_price3').val())/price1)*10,1));
		$('#medium_book_discount4').text(toDecimal((parseFloat($('#medium_book_price4').val())/price1)*10,1));
		$('#medium_book_discount5').text(toDecimal((parseFloat($('#medium_book_price5').val())/price1)*10,1));
		$('#medium_book_discount6').text(toDecimal((parseFloat($('#medium_book_price6').val())/price1)*10,1));
		return false;
    });
	//   $('#allPrice').on('click', function(){
    //   var price1 = parseFloat($('#old_price ').val());
    //   var price2 = parseFloat($('#recyle_price ').val());
    //   var allPrice1 = ((price2*2)+(price1*0.2)).toFixed(1);
    //   var allPrice2 = (price2*2).toFixed(1);
    //   var allPrice3 = (price2*2*0.75).toFixed(1);
    //   $('#new_book_price ').val(allPrice1);
    //   $('#good_book_price ').val(allPrice2);
    //   $('#medium_book_price ').val(allPrice3);
    //   $('#new_book_price_discount').text((((parseFloat($('#new_book_price').val()) / parseFloat($('#old_price').val())) * 10)).toFixed(1));
	// 		$('#good_book_price_discount').text((((parseFloat($('#good_book_price').val()) / parseFloat($('#old_price').val())) * 10)).toFixed(1));
	// 		$('#medium_book_price_discount').text((((parseFloat($('#medium_book_price').val()) / parseFloat($('#old_price').val())) * 10)).toFixed(1));
	// 		return false;
    // });
	  // 检测ISBN
	  $('#detection').on('click', function(){
          //执行 Ajax 后重载
		    $.ajax({
			    type: "POST",
			    dataType: "json",
			    url: "https://bookshop.dingdangtoy.com/api/admin/book/check_isbn",
			    data: {isbn: $('#isbn').val()},
			    headers: { //通过 request 头传递
		          'access-token': layui.data('layuiAdmin').access_token
		        },
				success: function(data){
		      if(data.message){
		      	$('#msg_info').text('库中已存在')
		      	$("#msg_info").css("color","red");
		      } else {
		      	$('#msg_info').text('库中不存在')
		      	$("#msg_info").css("color","blue");
		      }
				},
				error: function(msg){
				  console.log(msg)
				}
			});
			return false;
  });
  // 富文本
        var E = window.wangEditor
        var editor2 = new E('#editor2')
        editor2.customConfig.onchange = function (html) {
            // 监控变化，同步更新到 textarea
            $('#catalog').val(html)
        }
				editor2.create()
        var editor1 = new E('#editor1')
        editor1.customConfig.onchange = function (html) {
            // 监控变化，同步更新到 textarea
            $('#desc').val(html)
        }
				editor1.create()
        var editor = new E('#editor')
        editor.customConfig.onchange = function (html) {
            // 监控变化，同步更新到 textarea
            $('#author_intro').val(html)
        }
        editor.create()
  });
</script>
<style>
    .layui-form-label {text-align: center; width: 170px;}
    .int1{position: relative;padding-left: 30px;}
	.sp3{position: absolute;left: 10px;top: 10px;z-index: 10;color: #999;}
	.conter{width: 90%;overflow: auto;clear: both;margin: 0 auto}
	.bookstype{width: 33%;float: left;}
	.bookstype dt{width: 100%;height: 50px;background: #ccc;text-align: center;line-height: 50px;font-weight: bold;border: 1px solid #d8d8d8}
	.bookstype dd{width: 100%;height: 50px;text-align: center;line-height: 50px;border: 1px solid #d8d8d8}
	.bookstype dd input{text-align: center}
	.sp6{color:#999 !important}
</style>
